
#define HORIZONTAL_SYNCHRONOUS_PULSE  1
#define HORIZONTAL_BACK_PORCH         46
#define ACTIVE_WIDTH                  800
#define HORIZONTAL_FRONT_PORCH        210

#define VERTICAL_SYNCHRONOUS_PULSE    1
#define VERTICAL_BACK_PORCH           23
#define ACTIVE_HEIGHT                 480
#define VERTICAL_FRONT_PORCH          22

#define LCD_HS_CNT  ACTIVE_WIDTH
#define LCD_VS_CNT  ACTIVE_HEIGHT

//LCD?????????	   
uint16_t POINT_COLOR=LCD_COLOR_RED;	//????
uint16_t BACK_COLOR=0x00;  //??? 
#define FRAMEBUF  ((uint32_t)0xC0000000)
uint16_t *framebuf =(uint16_t*)FRAMEBUF;


static void tli_config(void);
static void tli_gpio_config(void);

void TLI_Init(void)
{
	tli_layer_lut_parameter_struct lut_struct;
	uint8_t i;

    tli_config();
    /* layer0 enable */
    tli_layer_enable(LAYER0);
    /* reload layer0 configuration */
    tli_reload_config(TLI_REQUEST_RELOAD_EN);
    tli_enable();
}


/*!
    \brief      LCD configuration 
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void tli_config(void)
{
    tli_parameter_struct               tli_init_struct;
    tli_layer_parameter_struct         tli_layer_init_struct;

    rcu_periph_clock_enable(RCU_TLI);
    tli_gpio_config();  
    /* configure the TLI clock to generate LCD clock */
    rcu_pllt_config(RCU_PLLTSRC_HXTAL);
    if(ERROR == rcu_pllt_vco_config(8, 150, 3)){
        while(1);
    }
    rcu_tli_clock_config(RCU_CKTLI_CKPLLTR_DIV2);

    rcu_osci_on(RCU_PLLT_CK);
    if(ERROR == rcu_osci_stab_wait(RCU_PLLT_CK)){
        while(1);
    }

    /* TLI initialization */
    tli_init_struct.signalpolarity_hs = TLI_HSYN_ACTLIVE_LOW;
    tli_init_struct.signalpolarity_vs = TLI_VSYN_ACTLIVE_LOW;
    tli_init_struct.signalpolarity_de = TLI_DE_ACTLIVE_LOW;
    tli_init_struct.signalpolarity_pixelck = TLI_PIXEL_CLOCK_TLI;
    
    /* LCD display timing configuration */
    tli_init_struct.synpsz_hpsz = HORIZONTAL_SYNCHRONOUS_PULSE - 1;
    tli_init_struct.synpsz_vpsz = VERTICAL_SYNCHRONOUS_PULSE - 1;
    tli_init_struct.backpsz_hbpsz = HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH - 1;
    tli_init_struct.backpsz_vbpsz = VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH - 1;
    tli_init_struct.activesz_hasz = HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH + ACTIVE_WIDTH - 1;
    tli_init_struct.activesz_vasz = VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH + ACTIVE_HEIGHT - 1;
    tli_init_struct.totalsz_htsz = HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH + ACTIVE_WIDTH + HORIZONTAL_FRONT_PORCH - 1;
    tli_init_struct.totalsz_vtsz = VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH + ACTIVE_HEIGHT + VERTICAL_FRONT_PORCH - 1;
    /* LCD background color configure*/
    tli_init_struct.backcolor_red = 0xFF;
    tli_init_struct.backcolor_green = 0xFF;
    tli_init_struct.backcolor_blue = 0xFF; 
    tli_init(&tli_init_struct);
  
    /* tli layer0 configuration */
		tli_layer_init_struct.layer_window_leftpos = (HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH);
		tli_layer_init_struct.layer_window_rightpos = (ACTIVE_WIDTH + HORIZONTAL_SYNCHRONOUS_PULSE + HORIZONTAL_BACK_PORCH - 1);
		tli_layer_init_struct.layer_window_toppos = (VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH);
		tli_layer_init_struct.layer_window_bottompos = (ACTIVE_HEIGHT + VERTICAL_SYNCHRONOUS_PULSE + VERTICAL_BACK_PORCH - 1);
		tli_layer_init_struct.layer_ppf = LAYER_PPF_RGB565;
		tli_layer_init_struct.layer_sa = 0xFF;
		tli_layer_init_struct.layer_default_blue = 0xFF;        
		tli_layer_init_struct.layer_default_green = 0xFF;       
		tli_layer_init_struct.layer_default_red = 0xFF;         
		tli_layer_init_struct.layer_default_alpha = 0xFF;
		tli_layer_init_struct.layer_acf1 = LAYER_ACF1_PASA;    
		tli_layer_init_struct.layer_acf2 = LAYER_ACF2_PASA;
		tli_layer_init_struct.layer_frame_bufaddr = FRAMEBUF;
		tli_layer_init_struct.layer_frame_line_length = ((ACTIVE_WIDTH * 2) + 3); 
		tli_layer_init_struct.layer_frame_buf_stride_offset = (ACTIVE_WIDTH * 2);
		tli_layer_init_struct.layer_frame_total_line_number = ACTIVE_HEIGHT; 
    tli_layer_init(LAYER0, &tli_layer_init_struct);
    tli_dither_config(TLI_DITHER_ENABLE);
}


/*!
    \brief      TLI GPIO configure 
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void tli_gpio_config(void)
{
    /* GPIO clock enable */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_GPIOD);
    rcu_periph_clock_enable(RCU_GPIOE);
    rcu_periph_clock_enable(RCU_GPIOF);
    rcu_periph_clock_enable(RCU_GPIOG);
    rcu_periph_clock_enable(RCU_AF);
    
    /* TLI GPIO pin remap configuration */
    gpio_pin_remap1_config(GPIO_PCF3, AFIO_PCF3_TLI_R7_PG6_REMAP     |  GPIO_PCF3_TLI_R6_PA8_REMAP      |
                                      AFIO_PCF3_TLI_R5_PA12_REMAP    |  GPIO_PCF3_TLI_R4_PA11_REMAP     |
                                      GPIO_PCF3_TLI_R3_PB0_REMAP     |
																		 
																		  GPIO_PCF3_TLI_G7_PD3_REMAP     |  GPIO_PCF3_TLI_G6_PC7_REMAP      | 
																			GPIO_PCF3_TLI_G5_PB11_REMAP    |  GPIO_PCF3_TLI_G4_PB10_REMAP     |
																			GPIO_PCF3_TLI_G3_PG10_REMAP    |  GPIO_PCF3_TLI_G2_PA6_REMAP      | 
																		
																			GPIO_PCF3_TLI_B7_PB9_REMAP     |  GPIO_PCF3_TLI_B6_PB8_REMAP      | 
																			GPIO_PCF3_TLI_B5_PA3_REMAP     |      
																			GPIO_PCF3_TLI_B3_PG11_REMAP    |
																		 
																		  GPIO_PCF3_TLI_CLK_PG7_REMAP    |
																		  GPIO_PCF3_TLI_DE_PF10_REMAP    |
																		  GPIO_PCF3_TLI_VSYNC_PA4_REMAP  |  
																			GPIO_PCF3_TLI_HSYNC_PC6_REMAP   ,ENABLE);
		gpio_pin_remap1_config(GPIO_PCF4, GPIO_PCF4_TLI_B4_PG12_REMAP   ,ENABLE);
    /* configure HSYNC(PC6), VSYNC(PA4), PCLK(PG7) */ 
    gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6 |GPIO_PIN_8 |GPIO_PIN_11 |GPIO_PIN_12); 

    gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10| GPIO_PIN_11);     

    gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6 | GPIO_PIN_7);  
    
    gpio_init(GPIOD,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_3);  
    
   // gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);  
   
    gpio_init(GPIOF,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10);  

    gpio_init(GPIOG,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6| GPIO_PIN_7 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);  
    /* back light */
    gpio_init(GPIOA, GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_2);
    gpio_bit_set(GPIOA,GPIO_PIN_2);
}


//画点
//x,y:坐标
//POINT_COLOR:此点的�?�色
void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t color)
{
	uint32_t potsion;
	if(x>ACTIVE_WIDTH || y>LCD_VS_CNT)return;
	potsion = (y*LCD_HS_CNT +x);								//正向显示
//		potsion = (LCD_VS_CNT-y)*LCD_HS_CNT +(LCD_HS_CNT-x);		//反向显示
	(uint16_t)*(framebuf+potsion) = color;
}
//清屏函数
//color:要清屏的�?充色
void LCD_Clear(uint16_t color)
{
	uint32_t index = 0;
	for (index = 0x00; index < LCD_HS_CNT*LCD_VS_CNT; index++){
			*(__IO uint16_t*)(framebuf + index) = color;
	}
}


uint16_t LCD_GetPoint(uint16_t x,uint16_t y)
{
	uint32_t potsion;
	uint16_t point;
	if(x>LCD_HS_CNT || y>LCD_VS_CNT)return 0;
	potsion = (y*LCD_HS_CNT +x);								//正向显示
//		potsion = (LCD_VS_CNT-y)*LCD_HS_CNT +(LCD_HS_CNT-x);		//反向显示
	point = (uint16_t)*(framebuf+potsion);
	return point;
}
